﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RegisterationCommon.Common;
using RegisterationCommon.School;
using RegisterationCommon.Student;
using System.Data;

namespace RegisterationSystem.Space_School
{
    public partial class students_register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //添加
        protected void btnadd_Click(object sender, EventArgs e)
        {
            DataTable dt = DBAction.GetDTFromSQL("select * from Students where IDCardNumber='" + txtIDCardNumber.Value.Trim() + "'");
            if (dt.Rows.Count > 0)
            {
                //已登记
                Message.Send(10019);
                return;
            }
            StudentInfo.Current.TrueName = txtname.Value.Trim();//学生姓名
            StudentInfo.Current.IDCardNumber = txtIDCardNumber.Value.Trim();//学生身份证
            StudentInfo.Current.Sex = ddlsex.SelectedValue == "0" ? false : true;//性别0男1女
            //StudentInfo.Current.Hobby = txtHouseAreaInfo.Value;//兴趣爱好
            StudentInfo.Current.FirstGuardianName = txtFirstGuardianName.Value;//第一监护人姓名
            StudentInfo.Current.FirstGuardianUnit = txtFirstGuardianUnit.Value;//第一监护人工作单位
            StudentInfo.Current.FirstGuardianPhone = txtFirstGuardianPhone.Value;//第一监护人联系电话
            StudentInfo.Current.FirstRelationship = txtFirstRelationship.Value;  //第一监护人和他的关系
            StudentInfo.Current.SecGuardianName = txtSecGuardianName.Value;//第二监护人姓名
            StudentInfo.Current.SecGuardianUnit = txtSecGuardianUnit.Value;//第二监护人工作单位
            StudentInfo.Current.SecGuardianPhone = txtSecGuardianPhone.Value;//第二监护人联系电话
            StudentInfo.Current.SecRelationship = txtSecRelationship.Value;  //第二监护人和他的关系
            StudentInfo.Current.SchID =0;//所属学校
            StudentInfo.Current.Status =0;//信息状态(0未分配1已分配2初审通过3初审不通过)
            StudentInfo.Current.RegisterTime = DateTime.Now;//登记时间
            StudentInfo.Current.PhoneStatusText = "";//电话回访反馈说明
            StudentInfo.Current.PhoneStatus = false;//电话回访状态(0未回访1回访)
            StudentInfo.Current.IsDelete = false;//是否删除(0未删除 1删除)
            StudentInfo.Current.IsRecord = true;//是否代录
            string NewPwd = StudentInfo.Current.IDCardNumber.Substring(6, 8);//截取身份证生日作为密码
            StudentInfo.Current.LoginPswd = BaseLibrary.ConvertToMD5(NewPwd.ToLower());//保存密码
            //获取身份证上的时间 进行比较 是否足龄
            string stuDate = StudentInfo.Current.IDCardNumber.Substring(6, 4) + "-" + StudentInfo.Current.IDCardNumber.Substring(10, 2) + "-" + StudentInfo.Current.IDCardNumber.Substring(12, 2);
            StudentInfo.Current.Birthday = Convert.ToDateTime(stuDate); //出生年月
            //足龄条件
            string yea = (DateTime.Now.Year - 6) + "-09-01";
            //身份证时间和足龄条件进行对比
            int isyea = DateTime.Compare(Convert.ToDateTime(yea), Convert.ToDateTime(stuDate));
            StudentInfo.Current.IsMatured = isyea > 0 ? true : false;//是否足龄
            StudentInfo.Current.HouseAreaID = Convert.ToInt32(hidhous.Value);   //户口所在地(0经济技术开发区1市内其它区2省内市外、外省外市)
            StudentInfo.Current.ScommAreaID = Convert.ToInt32(hidsid.Value);   //住房社区
            StudentInfo.Current.XcommAreaID = Convert.ToInt32(hidxid.Value);   //住房小区
            StudentInfo.Current.LevelNum = 0;   //住房社区
            StudentInfo.Current.ScommName = hidsname.Value;//社区名称
            StudentInfo.Current.XcommName = hidxname.Value;//小区名称
            StudentInfo.Current.SignUpNum = "42011111";//登记单号
            //StudentInfo.Current.HouseNumber = txtHouseNumber.Value;//门牌号
            StudentInfo.Current.HouseNumber = txtHouseNumber.Value ;//门牌号,如：M-M-M

            StudentInfo.Current.HouseCard = txtHouseCard.Value;//房产号
            StudentInfo.Current.Level = -1;//住房社区
            StudentInfo.Current.Adjustment = "";//是否服从调剂
            StudentInfo.Current.HouseIngoingTime = ddlHouseIngoingTime.Value;//户口迁入时间
            StudentInfo.Current.HousingOccupancyTime = HousingOccupancyTime.Value;//住房入住时间
            StudentInfo.Current.PermanentAddress = txtPermanentAddress.Value;//户籍地址
            StudentInfo.Current.ServicesDivision = 0;// 是否进行了服务划分（0，没有；1划分了）


            StudentInfo.Current.IsSelf = Convert.ToInt32(radio.Value);//是否为自有房产（0是1不是）
            StudentInfo.Current.Ext1 = txt.Value;//单独存储门栋，方便以后的服务划分，按照门栋
            StudentInfo.Current.Ext2 = "";
            StudentInfo.Current.Ext3 = "0";//0为代录
            StudentInfo.Current.Ext4 = "";//0为特殊人才
            StudentInfo.Current.Ext5 = "";//
            StudentInfo.Current.pianid = Convert.ToInt32(pianid.Value);//片区id
            StudentInfo.Current.pianname = pianname.Value;//片区名称
            switch (StudentInfo.Current.HouseAreaID)
            {
                case 0:
                    StudentInfo.Current.Level = 0;//学生等级
                    break;
                case 1:
                    StudentInfo.Current.Level = 1;//学生等级
                    break;
                case 2:
                    StudentInfo.Current.Level = 1;//学生等级
                    break;
                default:
                    StudentInfo.Current.Level = 0;//学生等级
                    break;
            }
            if (StudentInfo.Current.ScommName == "其他" && StudentInfo.Current.HouseAreaID == 2)
            {
                StudentInfo.Current.Level = 2;
                if (hidAdjustment.Value.ToString().Trim() == "0")
                {
                    StudentInfo.Current.Adjustment = "服从";
                }
                else
                {
                    StudentInfo.Current.Adjustment = hidAdjustment.Value.ToString().Trim();
                }
            }
            if (StudentInfo.Current.ScommName == "其他")
            {
                StudentInfo.Current.ServicesDivision = 1;
            }
            if (StudentInfo.Current.AddNewStudentInfo())
            {
                Message.Send(10020);
            }
            else
            {
                //失败
                Message.Send(10021);
            }
        }
    }
}